<?php
	require_once("includes/connect.php");
	require_once("includes/function.php");


	$tablename = $_POST["tablename"];
	$dd = $_POST["dd"];
	$fieldname = $_POST["fieldname"];
	$idname = $_POST["idname"];
	$by = $_POST["by"];

	$ex2_id = $_POST["ex2_id"];
	$ex3_id = $_POST["ex3_id"];

	$db = new myDBS;

	$where = "expenses1_id = $by";
	$result = $db->showData1($tablename, $where);
?>
<script type="text/javascript">
	$(document).ready(function(){
		$( "#<?php echo $dd; ?>" ).change(function() {
			var by= $(this).val();

			//alert(by);
			
			if (by != "เลือก...") {
				$.post('dropdown2.php', {  tablename: 'expenses_lvl3', dd: 'ex3_id', idname: 'expenses3_id', fieldname: 'expenses3_name', by: by, ex3_id:'<?php echo $ex3_id; ?>'}, function(data) { $("#expenses3").html(data); } );
			}
			
			
		});

		$( "#<?php echo $dd; ?>" ).trigger("change");
	});
</script>
<select class="span12" id="<?php echo $dd; ?>" name="<?php echo $dd; ?>" required>
	<option value="">เลือก...</option>
	<?php
		while ( $rows = $db->fetch_object ( $result )){
			if ($rows->{$idname} == $ex2_id) {
				$selected = " selected";
			} else {
				$selected = "";
			}
	?>
	<option value="<?php echo $rows->{$idname}; ?>" <?php echo $selected; ?>><?php echo $rows->{$fieldname}; ?></option>
	<?php } ?>
</select>